import type { App } from "vue"
import { useAuthStore } from "@/store/auth"
import { isContainArr } from "@/utils/validate"

/**
 * 自定义用户权限
 * @param app 
 */
export async function authDirective(app: App) {
  const authStore = useAuthStore()

  // 单个权限
  app.directive('auth', {
    mounted(el, binding) {
      const buttonList = authStore.buttonList
      if (!buttonList.some(btn => btn === binding.value)) {
        el.parentNode.removeChild(el)
      }
    }
  })

  // 只要满足其中一个
  app.directive('auths', {
    mounted(el, binding) {
      const buttonList = authStore.buttonList
      if (!buttonList.some(btn => binding.value.indexOf(btn) !== -1)) {
        el.parentNode.removeChild(el)
      }
    }
  })

  // 只要满足其中一个
  app.directive('authAll', {
    mounted(el, binding) {
      const buttonList = authStore.buttonList
      if (!isContainArr(buttonList, binding.value)) {
        el.parentNode.removeChild(el)
      }
    }
  })
}